package com.andova.component;

import android.app.Application;

import com.andova.component.config.AndovaConfigComponent;
import com.andova.component.im.IIMOption;
import com.andova.component.img.IImgOption;
import com.andova.component.lbs.AndovaLbsComponent;
import com.andova.component.log.AndovaLogComponent;
import com.andova.component.net.AndovaNetComponent;
import com.andova.component.net.NetworkSusComponent;
import com.andova.component.pic.AndovaPicComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-12-08.
 *
 * @author kzaxil
 * @since 1.0.1
 */
public class AndovaComponentManager {
    public static final String TAG = "AndovaComponent";
    private static AndovaComponentManager sComponent;
    private final List<AndovaComponent> mComponents = new ArrayList<>(5);

    /**
     * @deprecated Use {@link #getInstance()} instead.
     */
    public static AndovaComponentManager component() {
        if (sComponent == null) {
            synchronized (AndovaComponentManager.class) {
                if (sComponent == null) sComponent = new AndovaComponentManager();
            }
        }
        return sComponent;
    }

    public static AndovaComponentManager getInstance() {
        if (sComponent == null) {
            synchronized (AndovaComponentManager.class) {
                if (sComponent == null) sComponent = new AndovaComponentManager();
            }
        }
        return sComponent;
    }

    /**
     * 设置定位地图组件配置项
     */
    public AndovaComponentManager lbsComponent(AndovaLbsComponent option) {
        if (option == null) return this;
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaLbsComponent) mComponents.remove(module);
        }
        mComponents.add(option);
        return this;
    }

    /**
     * @throws NullPointerException If not set AndovaLbsComponent
     */
    public AndovaLbsComponent lbsComponent() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaLbsComponent) return (AndovaLbsComponent) module;
        }
        throw new NullPointerException("You should set AndovaLbsComponent object before initialize on Application!");
    }

    /**
     * 设置网络组件配置项
     */
    public AndovaComponentManager netComponent(AndovaNetComponent option) {
        if (option == null) return this;
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaNetComponent) mComponents.remove(module);
        }
        mComponents.add(option);
        return this;
    }

    /**
     * @throws NullPointerException If not set AndovaNetComponent
     */
    public AndovaNetComponent netComponent() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaNetComponent) return (AndovaNetComponent) module;
        }
        throw new NullPointerException("You should set AndovaNetComponent object before initialize on Application!");
    }

    /**
     * 设置网络组件配置项
     */
    public AndovaComponentManager netSusComponent(NetworkSusComponent option) {
        if (option == null) return this;
        for (AndovaComponent module : mComponents) {
            if (module instanceof NetworkSusComponent) mComponents.remove(module);
        }
        mComponents.add(option);
        return this;
    }

    /**
     * @throws NullPointerException If not set NetworkSusComponent
     */
    public NetworkSusComponent netSusComponent() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof NetworkSusComponent) return (NetworkSusComponent) module;
        }
        throw new NullPointerException("You should set NetworkSusComponent object before initialize on Application!");
    }

    /**
     * 设置图片组件配置项
     */
    public AndovaComponentManager picComponent(AndovaPicComponent component) {
        if (component == null) return this;
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaPicComponent) mComponents.remove(module);
        }
        mComponents.add(component);
        return this;
    }

    /**
     * @throws NullPointerException If not set AndovaPicComponent
     */
    public AndovaPicComponent picComponent() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaPicComponent) return (AndovaPicComponent) module;
        }
        throw new NullPointerException("You should set AndovaPicComponent object before initialize on Application!");
    }

    /**
     * 设置图片组件配置项
     */
    public AndovaComponentManager configComponent(AndovaConfigComponent component) {
        if (component == null) return this;
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaConfigComponent) mComponents.remove(module);
        }
        mComponents.add(component);
        return this;
    }

    /**
     * @throws NullPointerException If not set AndovaConfigComponent
     */
    public AndovaConfigComponent configComponent() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaConfigComponent) return (AndovaConfigComponent) module;
        }
        throw new NullPointerException("You should set AndovaConfigComponent object before initialize on Application!");
    }

    /**
     * 设置图片组件配置项
     */
    public AndovaComponentManager logComponent(AndovaLogComponent component) {
        if (component == null) return this;
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaLogComponent) mComponents.remove(module);
        }
        mComponents.add(component);
        return this;
    }

    /**
     * @throws NullPointerException If not set AndovaLogComponent
     */
    public AndovaLogComponent logComponent() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof AndovaLogComponent) return (AndovaLogComponent) module;
        }
        throw new NullPointerException("You should set AndovaLogComponent object before initialize on Application!");
    }

    /**
     * 设置图片组件配置项
     *
     * @deprecated Use {@link #picComponent(AndovaPicComponent)} instead.
     */
    public void imgOption(IImgOption option) {
        if (option == null) return;
        for (AndovaComponent module : mComponents) {
            if (module instanceof IImgOption) mComponents.remove(module);
        }
        mComponents.add(option);
    }

    /**
     * @deprecated Use {@link #picComponent()} instead.
     */
    public IImgOption imgOption() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof IImgOption) return (IImgOption) module;
        }
        return null;
    }

    /**
     * 设置即时通组件配置项
     */
    public void imOption(IIMOption option) {
        if (option == null) return;
        for (AndovaComponent module : mComponents) {
            if (module instanceof IIMOption) mComponents.remove(module);
        }
        mComponents.add(option);
    }

    public IIMOption imOption() {
        for (AndovaComponent module : mComponents) {
            if (module instanceof IIMOption) return (IIMOption) module;
        }
        return null;
    }

    private void onCreate(Application cxt) {
        if (cxt == null) return;
        for (AndovaComponent module : mComponents) {
            module.onCreate(cxt);
        }
    }

    public void onLowMemory(Application cxt) {
        if (cxt == null) return;
        for (AndovaComponent module : mComponents) {
            module.onLowMemory(cxt);
        }
    }

    public void initialize(Application cxt) {
        ObtainResourceKt.aa = cxt;
        onCreate(cxt);
    }
}
